{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import cv2\n",
    "from tqdm import tqdm\n",
    "import os\n",
    "import sys\n",
    "\n",
    "sys.path.append('..')\n",
    "from insightface.recognition.image_iter import FaceImageIter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "header0 label [5822654. 5908396.]\n",
      "id2range 85742\n",
      "5822653\n",
      "rand_mirror False\n"
     ]
    }
   ],
   "source": [
    "data_shape = (3,112,112)\n",
    "path_imgrec = os.path.join(\"/mnt/ssd2/Datasets/faces_emore\", \"train.rec\")\n",
    "\n",
    "train_dataiter = FaceImageIter(\n",
    "          batch_size           = 1,\n",
    "          data_shape           = data_shape,\n",
    "          path_imgrec          = path_imgrec,\n",
    "          shuffle              = True,\n",
    "          rand_mirror          = False,\n",
    "          mean                 = None,\n",
    "          cutoff               = False,\n",
    "          color_jittering      = 0,\n",
    "          images_filter        = 0,\n",
    "      )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "call reset()\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      "  0%|          | 0/490623 [00:00<?, ?it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "call reset()\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 490623/490623 [31:38<00:00, 258.36it/s] \n"
     ]
    }
   ],
   "source": [
    "train_dataiter.reset()\n",
    "dst_folder = \"/mnt/ssd2/Datasets/faces_emore_images\"\n",
    "labels_folder = \"/mnt/ssd2/Datasets/faces_emore_labels\"\n",
    "\n",
    "for num in tqdm(np.arange(train_dataiter.num_samples())):\n",
    "    data = train_dataiter.next()\n",
    "    img = np.transpose(data.data[0][0],(1, 2, 0)).asnumpy().astype(np.uint8)\n",
    "    label = data.label[0].asnumpy().astype(np.int32)\n",
    "    \n",
    "    np.save(os.path.join(labels_folder, str(num)+\"_label\"), label)\n",
    "    filename = os.path.join(dst_folder, str(num)+\".png\")\n",
    "    img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)\n",
    "    cv2.imwrite(filename, img)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
